/**
 * OSS is a Open Service Container which gives the leverage of building the Desktop and Web
 * Application. * Copyright (C) 2008-2009  Ponraj Sutanthiramani
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Or5e Open Source Community
 * 105, S.S Paradise Apartment, Munnireddy Layout, Horamavu, Banaswadi, Bangalore, India.
 */
package org.or5e.api.webcam.util;

import java.awt.*;
import java.awt.event.*;

public class CloseableFrame extends Frame {

	private static final long serialVersionUID = 1L;
	public boolean exitOnClose;
    public static boolean EXIT_DEFAULT = false;
    public static String DEFAULT_TITLE = "Closeable Frame";
    static int nFrames = 0; // used to displace successive frames
    static int maxCount = 20;
    static int perFrame = 15;

    public void cleanup() {
    }


    @SuppressWarnings("deprecation")
	public void center() {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frame = getSize();

        nFrames = (nFrames + 1) % maxCount;
        int offset = nFrames * perFrame;
        reshape(screen.width / 2 - frame.width / 2 + offset,
                screen.height / 2 - frame.height / 2 + offset,
                frame.width, frame.height);

    }

    @SuppressWarnings("deprecation") public void fullScreen() {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        reshape(0, 0, screen.width, screen.height);
    }

    public boolean canClose() {
        // override this to query the user etc before closing
        return true;
    }

    public CloseableFrame() {
        this(DEFAULT_TITLE, EXIT_DEFAULT);
    }

    public CloseableFrame(String title) {
        this(title, EXIT_DEFAULT);
    }

    public CloseableFrame(String title, boolean exit) {
        super(title);
        exitOnClose = exit;
        addWindowListener(new Closer());
    }

    class Closer extends WindowAdapter {

        public Closer() {
        }

        @Override
        public void windowClosing(WindowEvent e) {
            tryClose();
        }
    }
    @SuppressWarnings("deprecation") public CloseableFrame(Component comp, String title, boolean exit) {
        this(title, exit);
        add(BorderLayout.CENTER, comp);
        pack();
        show();
        repaint();
    }

    public void tryClose() {
        if (canClose()) {
            cleanup();
            if (exitOnClose) {
                System.exit(0);
            } else {
                dispose();
            }
        }
    }

    static class GreenComponent extends Component {
		private static final long serialVersionUID = -6479866766857760406L;

		public GreenComponent() {
        }

        @Override
        public void paint(Graphics g) {
            Dimension d = getSize();
            g.setColor(Color.green);
            g.fillRect(0, 0, d.width, d.height);
        }
    }

    public static void main(String[] args) throws Exception {
        new CloseableFrame(new GreenComponent(), "Closeable Frame Test", true).fullScreen();

    }
}
